package com.jysz.certificate.persistence.dao.system;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.jysz.certificate.persistence.dao.AbstractBaseDao;
import com.jysz.certificate.persistence.model.system.Menu;

/**
 * @author <a href="mailto:javaengine@outlook.com">Kaca Rich</a>
 *
 */
@Repository
public class MenuDao extends AbstractBaseDao<Menu, Long> {

	public Integer getMaxOrder(Menu menu) {
		Criteria criteria = this.createCriteria();
		criteria.setProjection(Projections.max(Menu.COLUMN_ORDER));
		if(menu != null){
			criteria.add(Restrictions.eq(Menu.COLUMN_PARENT, menu));
		}else{
			criteria.add(Restrictions.eq(Menu.COLUMN_GRADE, 1));
		}
		Object result = criteria.uniqueResult();
		if(result == null || Integer.valueOf(result.toString()) == 0){
			return 0;
		}
		
		return Integer.valueOf(result.toString());
	}

	@SuppressWarnings("unchecked")
	public List<Menu> findTopMenus() {
		Criteria criteria = this.createCriteria();
		criteria.add(Restrictions.eq(Menu.COLUMN_GRADE, 1));
//		criteria.addOrder(Order.asc(Menu.COLUMN_CODE));
		criteria.addOrder(Order.asc(Menu.COLUMN_ORDER));
		
		return criteria.list();
	}

}
